package com.qishanor.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishanor.framework.util.GenUtil;
import com.qishanor.system.mapper.GeneratorMapper;
import com.qishanor.system.model.GenColumnEntity;
import com.qishanor.system.model.GenConfig;
import com.qishanor.system.service.GeneratorService;
import lombok.AllArgsConstructor;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**

 * 代码生成器
 */
@Service
@AllArgsConstructor
public class GeneratorServiceImpl implements GeneratorService {

	/**
	 * 分页查询表
	 * @param tableName 查询条件
	 * @param dsName
	 * @return
	 */
	@Override
	public IPage<Map<String, Object>> getPage(Page page, String dsName, String tableName) {
		GeneratorMapper mapper = GenUtil.getMapper(dsName);
		// 手动切换数据源
		DynamicDataSourceContextHolder.push(dsName);
		return mapper.queryTable(page, tableName);
	}


	/**
	 * 查询一个表的所有列信息
	 * @param page
	 * @param genConfig 查询条件
	 * @return
	 */
	@Override
	public IPage<GenColumnEntity> listTableColumn(Page page, GenConfig genConfig) {
		GeneratorMapper mapper = GenUtil.getMapper(genConfig.getDsName());
		IPage<GenColumnEntity> columnPage = mapper.selectTableColumn(page, genConfig.getTableName(), genConfig.getDsName());

		// 处理 数据库类型和 Java 类型关系
		Configuration config = GenUtil.getConfig();
		columnPage.getRecords().forEach(column -> {
			// 只保留 （）之前部分，例如 timestamp(6) -> timestamp
			String dataType = StrUtil.subBefore(column.getDataType(), "(", false);
			String attrType = config.getString(dataType, "unknowType");
			column.setLowerAttrName(StringUtils.uncapitalize(GenUtil.columnToJava(column.getColumnName())));
			column.setJavaType(attrType);
		});
		return columnPage;
	}

	/**
	 * 生成代码
	 * @param genConfig 生成配置
	 * @return
	 */
	@Override
	public byte[] generatorCode(GenConfig genConfig) {

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		String dsName = genConfig.getDsName();
		String tableNames = genConfig.getTableName();
		GeneratorMapper mapper = GenUtil.getMapper(dsName);
		for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
			// 查询表信息
			Map<String, String> table = mapper.queryTable(tableName, dsName);
			// 查询列信息
			List<Map<String, String>> columns = mapper.selectMapTableColumn(tableName, dsName);
			// 生成代码
			GenUtil.generatorCode(genConfig, table, columns, zip);

		}
		IoUtil.close(zip);
		return outputStream.toByteArray();
	}

	@Override
	public Map<String, String> previewCode(GenConfig genConfig) {
		// 根据tableName 查询最新的表单配置
		String tableNames = genConfig.getTableName();
		String dsName = genConfig.getDsName();
		GeneratorMapper mapper = GenUtil.getMapper(genConfig.getDsName());
		for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
			// 查询表信息
			Map<String, String> table = mapper.queryTable(tableName, dsName);
			// 查询列信息
			List<Map<String, String>> columns = mapper.selectMapTableColumn(tableName, dsName);
			// 生成代码
			return GenUtil.generatorCode(genConfig, table, columns, null);

		}
		return MapUtil.empty();
	}



}
